<?php
/**
 * PhpStorm
 * User: zucheng
 * Date: 2019/3/24
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\Allowance;
use App\Models\Shipping;
use Validator;
use App\Http\Controllers\Api\ApiController;
use App\Models\Card;
use Illuminate\Http\Request;

class CardController extends ApiController
{
    /**
     * 获取油卡列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardList(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'type'        => 'required',
                'shipping_no' => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }
            // 获取当前可分配的新的油卡信息
            if ($request->get('type') == 1) {
                return $this->enabledCardList($request);
            }

            // 获取当前货主已分配给司机的油卡「还未退还的」
            if ($request->get('type') == 2) {
                return $this->usedCardList($request);
            }

            return $this->error('未知类型');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 获取当前可分配的新的油卡信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function enabledCardList(Request $request)
    {
        try {
            $cardNumbers = Card::query()
                ->where('card_status', Card::CARD_STATUS_ENABLED)
                ->pluck('card_number')
                ->toArray();

            return $this->success('success', $cardNumbers);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 获取当前货主已分配给司机的油卡「还未退还的」
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function usedCardList(Request $request)
    {
        try {
            //获取当前运单对应的货主和司机信息
            $shipping = Shipping::query()
                ->leftJoin('v_orders', 'v_orders.id', '=', 'v_shipping.order_id')
                ->where('shipping_no', $request->get('shipping_no'))
                ->select(['v_shipping.driver_uuid', 'v_orders.owner_uuid'])
                ->first();
            if (empty($shipping)) {
                return $this->success('当前司机没有已绑定的油卡', []);
            }

            //获取当前货主给司机分配的所有在用的油卡
            $cardNumbers = Shipping::query()
                ->leftJoin('v_orders', 'v_orders.id', '=', 'v_shipping.order_id')
                ->leftJoin('v_allowance', 'v_allowance.shipping_id', '=', 'v_shipping.id')
                ->where('v_shipping.driver_uuid', $shipping->driver_uuid)
                ->where('v_orders.owner_uuid', $shipping->owner_uuid)
                ->where('v_allowance.status', Allowance::STATUS_USEING)
                ->pluck('v_allowance.card_number')
                ->toArray();

            return $this->success('success', $cardNumbers);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}